import { _decorator, Component, Label, Node } from 'cc';
import { Skill } from './Skill';
const { ccclass, property } = _decorator;

@ccclass('SkillCard')
export class SkillCard extends Component {

    @property(Node) ndIcon: Node;
    @property(Node) ndName: Node;
    @property(Node) ndDesc: Node;
    private _skill: Skill;
    private _onClick: Function;
    private _target: any;

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    start() {

    }

    update(deltaTime: number) {
        
    } 

     setSkill(skill: Skill) {
        this._skill = skill;
        // 安全检查
        if(!skill) {
            return
        }

        this.ndName.getComponent(Label).string = skill.getName();
        this.ndDesc.getComponent(Label).string = skill.getDescription();
        
        // TODO 加载技能图标
        // this.ndIcon


    }

    onTouchStart(){
        this.node.setScale(0.9,0.9);
    }

    onTouchEnd(){
        this.node.setScale(1,1);
        this._onClick && this._onClick.apply(this._target);
    }

    onTouchCancel(){
        this.node.setScale(1,1)
    }


    onClick(onClick: Function, target?:any) {
        this._onClick = onClick;
        this._target = target;
    }


}


